﻿using EnterpriseAssets.Models;
using System;
using System.Web.Mvc;

namespace EnterpriseAssets.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()//首页
        {
            ViewBag.msg = ViewData["msg"]; //由Save传入保存成功信息
            return View();
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(FormCollection model)
        {
            FixedAssetInfo fixedAsset = new FixedAssetInfo();
            fixedAsset.Id = Guid.NewGuid();
            fixedAsset.Brand = model["Brand"];//获取客户端数据
            fixedAsset.Model = model["Model"];
            fixedAsset.AssetType = model["AssetType"];
            fixedAsset.UsingName = model["UsingName"];
            fixedAsset.UsingNameDepartment = model["UsingNameDepartment"];
            fixedAsset.SerialNumber = model["SerialNumber"];
            fixedAsset.ServiceNumber = model["ServiceNumber"];

            using (CodeFirstContext db = new CodeFirstContext())//插入保存数据
            {
                db.fixedAssetInfo.Add(fixedAsset);
                db.SaveChanges();
            }
            ViewData["msg"] = "数据保存成功！"; //提示保存成功消息，传给Index视图
            return View("Index");
        }

    }
}